package com.android.lovely.ui.widget

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.lovely.data.CreateImageDetail
import com.android.lovely.data.ImageGenerateData
import com.android.lovely.data.ParameterValue
import com.android.lovely.databinding.AdapterLoadingFooterBinding
import com.android.lovely.databinding.ItemKeyBinding
import com.android.lovely.databinding.ViewTextCreateBinding
import com.android.lovely.ext.TAG
import com.android.lovely.framework.BaseAdapter
import com.android.lovely.room.chat.database.BaseMessage
import com.kotlin.extensions.inflater.LayoutInflaterExt.layoutInflater

/**
 *
 * @author why
 * @date 2025/4/11 11:46
 */
class AICreateKeyWidget(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs)  {

    private val mBinding by lazy {
        ViewTextCreateBinding.inflate(layoutInflater, this, false)
    }

    init {
        addView(mBinding.root)
    }

    fun clear() {
        (0 until mBinding.root.childCount).map {
            (mBinding.root.getChildAt(it) as? CreateKeyWidget)?.clear()
        }
    }

    fun refresh(isNsfw: Boolean) {
        (0 until mBinding.root.childCount).map {
            (mBinding.root.getChildAt(it) as? CreateKeyWidget)?.refresh(isNsfw)
        }
    }

    fun check(): CreateKeyWidget? {
        return (0 until mBinding.root.childCount)
            .map { mBinding.root.getChildAt(it) as? CreateKeyWidget }
            .firstOrNull { !(it?.validateInput() ?: false) }
    }

    fun getAllSelected(): Map<String, List<String>> {
        return (0 until mBinding.root.childCount)
            .mapNotNull {  mBinding.root.getChildAt(it) as? CreateKeyWidget }
            .associate { it.getSelectedParameters() }
    }

    /**
     * 初始化视图
     * @param isNsfw 是否为 NSFW 内容
     * @param details 要显示的生成详情列表
     */
    fun initView(isNsfw: Boolean, details: List<CreateImageDetail>) {
        details.forEach { detail ->
            context?.let {
                CreateKeyWidget(it,null).apply {
                    updateContent(isNsfw, detail)
                    mBinding.root.addView(this)
                }
            }
        }
    }

    /**
     * 使用 AI 生成的内容更新所有子视图
     * @param details AI 生成的详情列表
     */
    fun aiWrite(details: List<CreateImageDetail>?) {
        (0 until mBinding.root.childCount).map {
            (mBinding.root.getChildAt(it) as? CreateKeyWidget)?.applyAISuggestions(details)
        }
    }

    /**
     * 使用历史数据更新所有子视图
     * @param params 历史参数映射
     */
    fun historyWrite(params: Map<String, List<String>>) {
        (0 until mBinding.root.childCount).map {
            val item = (mBinding.root.getChildAt(it) as? CreateKeyWidget)
            item?.getDetail()?.let { detail ->
                params[detail.codeValue]?.let { values ->
                    Log.d(TAG, "Writing history params: $values")
                    item.applyHistoryData(values.map { ParameterValue(it, it) })
                }
            }
        }
    }
}